package im.vinci.server.feed.persistence;

import im.vinci.server.feed.domain.wrapper.UserPlayInfoDAO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Created by mingjie on 17/2/15.
 */
@Repository
public interface UserPlayInfoMapper {

    @Select("SELECT user_id,music_info,create_time FROM user_play_info WHERE user_id = #{user_id} AND create_time > #{start_time} AND create_time < #{end_time} ORDER BY create_time ASC")
    List<UserPlayInfoDAO> select(@Param("user_id") long userId, @Param("start_time") Date startTime, @Param("end_time") Date endTime);

    @Insert("INSERT INTO user_play_info(user_id,song_id,song_type,music_info,create_time) VALUES(#{item.userId},#{item.songId},#{item.songType},#{item.musicInfo},#{item.createTime})")
    int insert(@Param("item") UserPlayInfoDAO userPlayInfoDAO);

    @Select({"<script>",
            "SELECT user_id,music_info FROM user_play_info WHERE user_id in ",
            "<foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "AND create_time &gt; #{start_time} AND create_time &lt; #{end_time}",
            "</script>"})
    List<UserPlayInfoDAO> batchSelect(@Param("list") Collection<Long> userIdList, @Param("start_time") Date startTime, @Param("end_time") Date endTime);
}
